<?php

/**
 * 会员注册
 */

namespace app\member\common;

class RegisterCommon extends \app\member\common\MemberCommon {

    protected $noLogin = true;
    protected $nameTip = '手机号/邮箱';


    public function __construct() {
        parent::__construct();
        $config = target('member/memberConfig')->getConfig();
        if($config['reg_type'] == 'all') {
            $this->nameTip = '手机号/邮箱';
        }
        if($config['reg_type'] == 'email') {
            $this->nameTip = '邮箱';
        }
        if($config['reg_type'] == 'tel') {
            $this->nameTip = '手机';
        }
    }

    protected function index() {
        $config = target('member/MemberConfig')->getConfig();
        if (!isPost()) {
            $this->setMeta('注册');
            $this->pageInfo['name'] = '注册';
            $hookHtml = '';
            if(LAYER_NAME == 'mobile') {
                $hookList = run('service', 'member', 'regMobileHtml');
            }else {
                $hookList = run('service', 'member', 'regHtml');
            }
            foreach ($hookList as $app => $vo) {
                if(!empty($vo)) {
                    $hookHtml .= $vo;
                }
            }

            $this->assign('nameTip', $this->nameTip);
            $this->assign('userConfig', $config);
            $this->assign('hookHtml', $hookHtml);
            $this->otherDisplay();
        } else {
            $userName = request('post', 'username');
            $password = request('post', 'password');
            $password2 = request('post', 'password2');
            $code = request('post', 'code');
            if (empty($userName) || empty($password)) {
                $this->error('用户名或密码未填写！');
            }
            if ($password <> $password2) {
                $this->error('重复密码不正确!');
            }
            if (!request('post', 'agreement')) {
                $this->error('请先阅读注册协议！');
            }

            if ($config['verify_image']) {
                if (!$this->getImgCode()->check(request('post', 'imgcode'))) {
                    $this->error('图片验证码输入不正确!');
                }
            }

            target('member/MemberUser')->beginTransaction();
            $loginData = target('member/Member', 'service')->regUser($userName, $password, $code);
            if (!$loginData) {
                target('member/MemberUser')->rollBack();
                $this->error(target('member/Member', 'service')->getError());
            }
            target('member/MemberUser')->commit();

            $loginData = target('member/Member', 'service')->loginUser($userName, $password);
            if (!$loginData) {
                $this->error(target('member/Member', 'service')->getError());
            }

            $loginCookie = [
                'uid' => $loginData['uid'],
                'token' => $loginData['token']
            ];
            $time = 2592000;
            \Dux::cookie()->set('user_login', $loginCookie, $time);
            $this->success('账号注册成功!', url('member/Index/index'));
        }
    }

    protected function forgot() {
        if (!isPost()) {
            $this->setMeta('找回密码');
            $this->pageInfo['name'] = '找回密码';
            $config = target('member/MemberConfig')->getConfig();
            $this->assign('userConfig', $config);
            $this->assign('nameTip', $this->nameTip);
            $this->otherDisplay();
        } else {
            $userName = request('post', 'username');
            $password = request('post', 'password');
            $password2 = request('post', 'password2');
            $code = request('post', 'code');
            if (empty($userName) || empty($password)) {
                $this->error('用户名或密码未填写！');
            }
            if ($password <> $password2) {
                $this->error('重复密码不正确!');
            }
            $loginData = target('member/Member', 'service')->forgotUser($userName, $password, $code);
            if (!$loginData) {
                $this->error(target('member/Member', 'service')->getError());
            }
            $this->success('密码修改成功!', url('member/Login/index'));
        }
    }

    protected function agreement() {
        $this->setMeta('注册协议');
        $this->pageInfo['name'] = '注册协议';
        $config = target('member/MemberConfig')->getConfig();
        $this->assign('userConfig', $config);
        $this->dialogDisplay();
    }

    public function code() {
        $this->getImgCode()->showImage();
    }

    protected function getCode() {
        if (!$this->getImgCode()->check(request('post', 'imgcode'))) {
            $this->error('图片验证码输入不正确!');
        }
        if (!target('member/Member', 'service')->getVerify(request('post', 'username'))) {
            $this->error(target('member/Member', 'service')->getError());
        }
        $this->success('验证码已发送,请注意查收!');
    }

}